package com.hitalkie.teacher.activity;

import android.Manifest;
import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.*;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.SensorManager;
import android.os.*;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.*;
import android.widget.*;
import com.google.gson.Gson;
import com.hitalkie.teacher.R;
import com.hitalkie.teacher.adapter.LessonHintRvAdapter;
import com.hitalkie.teacher.app.TalkieApi;
import com.hitalkie.teacher.model.BaseResponse;
import com.hitalkie.teacher.model.Lesson;
import com.hitalkie.teacher.tx.*;
import com.hitalkie.teacher.util.OkHttpManager;
import com.iflytek.cloud.*;
import com.tencent.TIMGroupMemberInfo;
import com.tencent.TIMUserProfile;
import com.tencent.av.TIMAvManager;
import com.tencent.av.sdk.AVView;
import com.tencent.av.utils.PhoneStatusTools;
import okhttp3.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


/**
 * Live直播类
 */
public class LiveActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = LiveActivity.class.getSimpleName();
    private static final int GETPROFILE_JOIN = 0x200;

    private EnterLiveHelper mEnterRoomHelper;
    private LiveHelper mLiveHelper;

    //    private ArrayList<ChatEntity> mArrayListChatEntity;
//    private ChatMsgListAdapter mChatMsgListAdapter;
    private static final int MINFRESHINTERVAL = 500;
    private static final int UPDAT_WALL_TIME_TIMER_TASK = 1;
    private static final int TIMEOUT_INVITE = 2;
    private boolean mBoolRefreshLock = false;
    private boolean mBoolNeedRefresh = false;
    private final Timer mTimer = new Timer();
    private ArrayList<ChatEntity> mTmpChatList = new ArrayList<ChatEntity>();//缓冲队列
    private TimerTask mTimerTask = null;
    private static final int REFRESH_LISTVIEW = 5;
    private Dialog mMemberDg, closeCfmDg, inviteDg;
    private ImageView mHeadIcon;
    private TextView mHostNameTv;
    private LinearLayout mHostLayout, mHostLeaveLayout;
    private final int REQUEST_PHONE_PERMISSIONS = 0;
    private long mSecond = 0;
    private long mMinute = 0;
    private String formatTime;
    private Timer mHearBeatTimer, mVideoTimer;

    private Timer mLessonStatusTimer;

    //    private TextView mVideoTime;
    private ObjectAnimator mObjAnim;
    private ImageView mRecordBall;
    private int thumbUp = 0;
    private long admireTime = 0;
    private int watchCount = 0;
    private static boolean mBeatuy = false;
    private static boolean mWhite = true;
    private boolean bCleanMode = false;
    private boolean mProfile;
    private boolean bFirstRender = true;
    private boolean bInAvRoom = false; // 完全进入房间后为true，完全退出房间后为false
    private boolean bSlideUp = false;
    private boolean bDelayQuit = false; // 完全进入房间后为true，按下后退时变为 false
    private boolean bReadyToChange = false;       // 正在切换房间
    private boolean isScreenShare = false;
    private String backGroundId;

    private TextView tvMembers;
    private TextView tvAdmires;

    private Dialog mDetailDialog;

    private ArrayList<String> mRenderUserList = new ArrayList<>();


    public static final String EXTRA_LESSON = "com.hitalkie.talkie.extra.LESSON";
    private Lesson mLesson;

    private boolean mIsRightShowing = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);   // 不锁屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setContentView(R.layout.activity_live);
        checkPermission();

        mLesson = (Lesson) getIntent().getSerializableExtra(EXTRA_LESSON);
        Log.e(TAG, "type :" + getIntent().getStringExtra("type"));

        //进出房间的协助类
        mEnterRoomHelper = new EnterLiveHelper(this, this);
        //房间内的交互协助类
        mLiveHelper = new LiveHelper(this, this);

        initView();
        registerReceiver();
        backGroundId = CurLiveInfo.getHostID();
        //进入房间流程
        mEnterRoomHelper.startEnterRoom();

        //QavsdkControl.getInstance().setCameraPreviewChangeCallback();
        mLiveHelper.setCameraPreviewChangeCallback();
        // 取消 视频跟着 屏幕 转动
//        registerOrientationListener();
//        startOrientationListener();

    }


    private boolean isRecognizeVoice = false;
    private SpeechRecognizer mIat;

    private void startRecognizeVoice() {
        //1.创建SpeechRecognizer对象，第二个参数：本地听写时传InitListener
        mIat = SpeechRecognizer.createRecognizer(getActivity(), null);
        //2.设置听写参数，详见《科大讯飞MSC API手册(Android)》SpeechConstant类
        mIat.setParameter(SpeechConstant.AUDIO_SOURCE, "1");// 默认值是1 ，通过手机麦克风
//        mIat.setParameter(SpeechConstant.AUDIO_SOURCE, "-1");// 通过writeAudio函数送入音频
        mIat.setParameter(SpeechConstant.DOMAIN, "iat");
        mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        mIat.setParameter(SpeechConstant.ACCENT, "mandarin ");
        mIat.setParameter(SpeechConstant.ASR_PTT, "0");
        mIat.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "false");
        //3.开始听写
        mIat.startListening(mRecoListener);
//        mLiveHelper.openTouchuan();
    }

    private void stopRecognizeVoice() {
        mIat.stopListening();
    }

    private RecognizerListener mRecoListener = new RecognizerListener() {
        //听写结果回调接口(返回Json格式结果，用户可参见附录13.1);
        // 一般情况下会通过onResults接口多次返回结果，完整的识别内容是多次结果的累加;
        // 关于解析Json的代码可参见Demo中JsonParser类;
        //isLast等于true时会话结束。
        public void onResult(RecognizerResult results, boolean isLast) {
            Log.e(TAG, "result:" + results.getResultString());
            Toast.makeText(getActivity(), results.getResultString(), Toast.LENGTH_SHORT).show();
        }

        public void onError(SpeechError error) {
            //会话发生错误回调接口
            //打印错误码描述
            Log.e(TAG, "error:" + error.getPlainDescription(true));
        }

        public void onBeginOfSpeech() {
            //开始录音
            isRecognizeVoice = true;
            Toast.makeText(getActivity(), "识别开始", Toast.LENGTH_SHORT).show();
        }

        public void onVolumeChanged(int volume, byte[] data) {
            //volume音量值0~30，data音频数据
        }

        public void onEndOfSpeech() {
            //结束录音
            isRecognizeVoice = false;
            Toast.makeText(getActivity(), "识别结束", Toast.LENGTH_SHORT).show();
        }

        public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
            //扩展用接口
        }
    };


    @Override
    protected void onResume() {
        super.onResume();
        QavsdkControl.getInstance().getAVContext().getAudioCtrl().enableSpeaker(true);
        mLiveHelper.resume();
        QavsdkControl.getInstance().onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (null != QavsdkControl.getInstance().getAVContext()) {
            // 帐号被踢下线时，AVContext为空
            QavsdkControl.getInstance().getAVContext().getAudioCtrl().enableSpeaker(false);
            mLiveHelper.pause();
            QavsdkControl.getInstance().onPause();
        }
    }

    @Override
    protected void onDestroy() {
        stopOrientationListener();
        watchCount = 0;
        super.onDestroy();
        if (null != mHearBeatTimer) {
            mHearBeatTimer.cancel();
            mHearBeatTimer = null;
        }
        if (null != mLessonStatusTimer) {
            mLessonStatusTimer.cancel();
            mLessonStatusTimer = null;
        }
        if (null != mVideoTimer) {
            mVideoTimer.cancel();
            mVideoTimer = null;
        }
        if (null != paramTimer) {
            paramTimer.cancel();
            paramTimer = null;
        }
        mLiveHelper.stopRecord();

        inviteViewCount = 0;
        thumbUp = 0;
        CurLiveInfo.setMembers(0);
        CurLiveInfo.setAdmires(0);
        CurLiveInfo.setCurrentRequestCount(0);
        unregisterReceiver();
        mLiveHelper.onDestory();
        mEnterRoomHelper.onDestory();
        QavsdkControl.getInstance().clearVideoMembers();
        QavsdkControl.getInstance().onDestroy();
    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case UPDAT_WALL_TIME_TIMER_TASK:
                    updateWallTime();
                    break;
//                case REFRESH_LISTVIEW:
//                    doRefreshListView();
//                    break;
                case TIMEOUT_INVITE:
                    String id = "" + msg.obj;
                    cancelInviteView(id);
                    // 取消邀请
                    mLiveHelper.sendGroupMessage(Constants.AVIMCMD_MULTI_HOST_CANCELINVITE, id);
                    break;
            }
            return false;
        }
    });

    /**
     * 直播心跳
     */
    private class HeartBeatTask extends TimerTask {
        @Override
        public void run() {
            String host = CurLiveInfo.getHostID();
            Log.i(TAG, "HeartBeatTask " + host);
//            OKhttpHelper.getInstance().sendHeartBeat(host, CurLiveInfo.getMembers(), CurLiveInfo.getAdmires(), 0);
        }
    }

    /**
     * 在线人数 记时器
     */
    private class LessonStatusTimerTask extends TimerTask {
        public void run() {
            Log.i(TAG, "HeadCountTimerTask ");
            if (MySelfInfo.getInstance().getIdStatus() == Constants.HOST) {
                getLessonStatus();
            }
        }
    }


    /**
     * 直播时间记时器
     */
    private class VideoTimerTask extends TimerTask {
        public void run() {
            Log.i(TAG, "timeTask ");
//            ++mSecond;
            ++mMinute;
            if (MySelfInfo.getInstance().getIdStatus() == Constants.HOST) {
                mHandler.sendEmptyMessage(UPDAT_WALL_TIME_TIMER_TASK);
            }
        }
    }

    /**
     * 时间格式化
     */
    private void updateWallTime() {
//        String hs, ms, ss;
//
//        long h, m, s;
//        h = mSecond / 3600;
//        m = (mSecond % 3600) / 60;
//        s = (mSecond % 3600) % 60;
//        if (h < 10) {
//            hs = "0" + h;
//        } else {
//            hs = "" + h;
//        }
//
//        if (m < 10) {
//            ms = "0" + m;
//        } else {
//            ms = "" + m;
//        }
//
//        if (s < 10) {
//            ss = "0" + s;
//        } else {
//            ss = "" + s;
//        }
//        if (hs.equals("00")) {
//            formatTime = ms + ":" + ss;
//        } else {
//            formatTime = hs + ":" + ms + ":" + ss;
//        }

//        if (Constants.HOST == MySelfInfo.getInstance().getIdStatus() && null != mVideoTime) {
//            Log.i(TAG, " refresh time ");
//            mVideoTime.setText(formatTime);
//        }

        if (Constants.HOST == MySelfInfo.getInstance().getIdStatus()) {
            Log.i(TAG, " refresh time " + mMinute);
            if (mMinute > mLesson.duration) {
                Toast.makeText(getActivity(), "Time to finish", Toast.LENGTH_LONG).show(); // TODO language
            }
        }
    }


    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Constants.ACTION_SURFACE_CREATED)) {
                //AvSurfaceView 初始化成功。 如果当前用户是主播则打开摄像头
                if (MySelfInfo.getInstance().getIdStatus() == Constants.HOST) {
                    mLiveHelper.openCameraAndMic();
                }

            }

            if (action.equals(Constants.ACTION_CAMERA_OPEN_IN_LIVE)) {
                //有人打开摄像头 (有人上麦！) 。来自AVRoomMulti.EventListener 的回调 onEndpointsUpdateInfo ：TYPE_MEMBER_CHANGE_HAS_CAMERA_VIDEO
                isScreenShare = false;
                ArrayList<String> ids = intent.getStringArrayListExtra("ids");
                //如果是自己本地直接渲染
                for (String id : ids) {
                    if (!mRenderUserList.contains(id)) {
                        mRenderUserList.add(id);
                    }
                    updateHostLeaveLayout();

                    if (id.equals(MySelfInfo.getInstance().getId())) {
                        showVideoView(true, id);
                        return;
//                        ids.remove(id);
                    }
                }
                //其他人一并获取
                Log.d(TAG, "ACTION_VIEWER_SHOW：" + MySelfInfo.getInstance().getId() + " ／ " + "somebody open camera,need req data" + " ／ " + "STATUS.SUCCEED" + " ／ " + "ids " + ids.toString());
                int requestCount = CurLiveInfo.getCurrentRequestCount();
                mLiveHelper.requestViewList(ids);
                requestCount = requestCount + ids.size();
                CurLiveInfo.setCurrentRequestCount(requestCount);
            }

            if (action.equals(Constants.ACTION_CAMERA_CLOSE_IN_LIVE)) {
                //有人关闭摄像头 (有人下麦！) 。来自AVRoomMulti.EventListener 的回调 onEndpointsUpdateInfo ：TYPE_MEMBER_CHANGE_NO_CAMERA_VIDEO
                // 或者有人取消分享屏幕 。来自AVRoomMulti.EventListener 的回调 onEndpointsUpdateInfo ：TYPE_MEMBER_CHANGE_NO_SCREEN_VIDEO
                ArrayList<String> ids = intent.getStringArrayListExtra("ids");
                //如果是自己本地直接渲染
                for (String id : ids) {
                    mRenderUserList.remove(id);
                }
                updateHostLeaveLayout();
            }

            if (action.equals(Constants.ACTION_SCREEN_SHARE_IN_LIVE)) {
                //有人分享屏幕
                ArrayList<String> ids = intent.getStringArrayListExtra("ids");
                //如果是自己本地直接渲染。来自AVRoomMulti.EventListener 的回调 onEndpointsUpdateInfo ：TYPE_MEMBER_CHANGE_HAS_SCREEN_VIDEO
                for (String id : ids) {
                    if (!mRenderUserList.contains(id)) {
                        mRenderUserList.add(id);
                    }
                    updateHostLeaveLayout();

                    if (id.equals(MySelfInfo.getInstance().getId())) {
                        showVideoView(true, id);
                        return;
//                        ids.remove(id);
                    }
                }
                //其他人一并获取
                Log.d(TAG, "ACTION_VIEWER_SHOW " + " ／ " + MySelfInfo.getInstance().getId() + " somebody open camera,need req data" + " ／ " + "STATUS.SUCCEED" + " ／ " + "ids " + ids.toString());
                int requestCount = CurLiveInfo.getCurrentRequestCount();
                mLiveHelper.requestScreenViewList(ids);
                isScreenShare = true;
                requestCount = requestCount + ids.size();
                CurLiveInfo.setCurrentRequestCount(requestCount);
            }


            if (action.equals(Constants.ACTION_SWITCH_VIDEO)) {
                // 点击成员回调
                backGroundId = intent.getStringExtra(Constants.EXTRA_IDENTIFIER);
                Log.v(TAG, "switch video enter with id:" + backGroundId);

                updateHostLeaveLayout();

                if (MySelfInfo.getInstance().getIdStatus() == Constants.HOST) {
                    //自己是主播
                    if (backGroundId.equals(MySelfInfo.getInstance().getId())) {
                        //背景是自己
//                        mHostCtrView.setVisibility(View.VISIBLE);
//                        mVideoMemberCtrlView.setVisibility(View.INVISIBLE);
                    } else {//背景是其他成员
//                        mHostCtrView.setVisibility(View.INVISIBLE);
//                        mVideoMemberCtrlView.setVisibility(View.VISIBLE);
                    }
                } else {
                    //自己是成员
                    if (backGroundId.equals(MySelfInfo.getInstance().getId())) {
                        //背景是自己
//                        mVideoMemberCtrlView.setVisibility(View.VISIBLE);
//                        mNomalMemberCtrView.setVisibility(View.INVISIBLE);
                    } else if (backGroundId.equals(CurLiveInfo.getHostID())) {
                        //主播自己
//                        mVideoMemberCtrlView.setVisibility(View.INVISIBLE);
//                        mNomalMemberCtrView.setVisibility(View.VISIBLE);
                    } else {
//                        mVideoMemberCtrlView.setVisibility(View.INVISIBLE);
//                        mNomalMemberCtrView.setVisibility(View.INVISIBLE);
                    }

                }

            }
            if (action.equals(Constants.ACTION_HOST_LEAVE)) {
                //主播结束
                quiteLivePassively();
            }


        }
    };

    private void registerReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constants.ACTION_SURFACE_CREATED);
        intentFilter.addAction(Constants.ACTION_HOST_ENTER);
        intentFilter.addAction(Constants.ACTION_HOST_LEAVE);
        intentFilter.addAction(Constants.ACTION_CAMERA_OPEN_IN_LIVE);
        intentFilter.addAction(Constants.ACTION_CAMERA_CLOSE_IN_LIVE);
        intentFilter.addAction(Constants.ACTION_SWITCH_VIDEO);
        intentFilter.addAction(Constants.ACTION_SCREEN_SHARE_IN_LIVE);
        registerReceiver(mBroadcastReceiver, intentFilter);

    }

    private void unregisterReceiver() {
        unregisterReceiver(mBroadcastReceiver);
    }

    /**
     * 初始化UI
     */
    private View avView;
    private TextView BtnBack, BtnInput, Btnflash, BtnSwitch, BtnBeauty, BtnWhite, BtnMic, BtnScreen, BtnHeart, mVideoChat, BtnCtrlVideo, BtnCtrlMic, BtnHungup, mBeautyConfirm;
    private TextView inviteView1, inviteView2, inviteView3;
    private LinearLayout mHostCtrView, mNomalMemberCtrView, mVideoMemberCtrlView, mBeautySettings;
    private View mFullControllerUi, mBackgound;
    private SeekBar mBeautyBar;
    private int mBeautyRate, mWhiteRate;
    private TextView pushBtn, recordBtn, speedBtn;

    private void showHeadIcon(ImageView view, String avatar) {
        if (TextUtils.isEmpty(avatar)) {
//            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.default_avatar);
//            Bitmap cirBitMap = UIUtils.createCircleImage(bitmap, 0);
//            view.setImageBitmap(cirBitMap);
        } else {
            Log.d(TAG, "load icon: " + avatar);
//            if (isDestroyed() == true) return;
//            RequestManager req = Glide.with(this);
//            req.load(avatar).transform(new GlideCircleTransform(this)).into(view);
        }
    }

    private void updateHostLeaveLayout() {
        if (MySelfInfo.getInstance().getIdStatus() == Constants.HOST) {
            return;
        } else {
            // 退出房间或主屏为主播且无主播画面显示主播已离开
            if (!bInAvRoom || (CurLiveInfo.getHostID().equals(backGroundId) && !mRenderUserList.contains(backGroundId))) {
                mHostLeaveLayout.setVisibility(View.VISIBLE);
            } else {
                mHostLeaveLayout.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 初始化界面
     */
    private void initView() {
//        mHostCtrView = (LinearLayout) findViewById(R.id.host_bottom_layout);
//        mNomalMemberCtrView = (LinearLayout) findViewById(R.id.member_bottom_layout);
//        mVideoMemberCtrlView = (LinearLayout) findViewById(R.id.video_member_bottom_layout);
        mHostLeaveLayout = (LinearLayout) findViewById(R.id.ll_host_leave);
//        mVideoChat = (TextView) findViewById(R.id.video_interact);
//        mVideoTime = (TextView) findViewById(R.id.broadcasting_time);
//        mHeadIcon = (ImageView) findViewById(R.id.head_icon);
//        mVideoMemberCtrlView.setVisibility(View.INVISIBLE);
//        mHostNameTv = (TextView) findViewById(R.id.host_name);
//        tvMembers = (TextView) findViewById(R.id.member_counts);
//        tvAdmires = (TextView) findViewById(R.id.heart_counts);
//
//        speedBtn = (TextView) findViewById(R.id.speed_test_btn);
//        speedBtn.setOnClickListener(this);
//
//        BtnCtrlVideo = (TextView) findViewById(R.id.camera_controll);
//        BtnCtrlMic = (TextView) findViewById(R.id.mic_controll);
//        BtnHungup = (TextView) findViewById(R.id.close_member_video);
//        BtnCtrlVideo.setOnClickListener(this);
//        BtnCtrlMic.setOnClickListener(this);
//        BtnHungup.setOnClickListener(this);
//        TextView roomId = (TextView) findViewById(R.id.room_id);
//        roomId.setText(CurLiveInfo.getChatRoomId());

        //for 测试用
        TextView paramVideo = (TextView) findViewById(R.id.param_video);
        paramVideo.setOnClickListener(this);
        tvTipsMsg = (TextView) findViewById(R.id.qav_tips_msg);
        tvTipsMsg.setTextColor(Color.GREEN);
        paramTimer.schedule(task, 1000, 1000);


        initBackDialog();
        if (MySelfInfo.getInstance().getIdStatus() == Constants.HOST) {
//            mHostCtrView.setVisibility(View.VISIBLE);
//            mNomalMemberCtrView.setVisibility(View.GONE);
//            mRecordBall = (ImageView) findViewById(R.id.record_ball);
//            Btnflash = (TextView) findViewById(R.id.flash_btn);
//            BtnSwitch = (TextView) findViewById(R.id.switch_cam);
//            BtnBeauty = (TextView) findViewById(R.id.beauty_btn);
//            BtnWhite = (TextView) findViewById(R.id.white_btn);
//            BtnMic = (TextView) findViewById(R.id.mic_btn);
//            BtnScreen = (TextView) findViewById(R.id.fullscreen_btn);
//            mVideoChat.setVisibility(View.VISIBLE);
//            Btnflash.setOnClickListener(this);
//            BtnSwitch.setOnClickListener(this);
//            BtnBeauty.setOnClickListener(this);
//            BtnWhite.setOnClickListener(this);
//            BtnMic.setOnClickListener(this);
//            BtnScreen.setOnClickListener(this);
//            mVideoChat.setOnClickListener(this);

//            inviteView1 = (TextView) findViewById(R.id.invite_view1);
//            inviteView2 = (TextView) findViewById(R.id.invite_view2);
//            inviteView3 = (TextView) findViewById(R.id.invite_view3);
//            inviteView1.setOnClickListener(this);
//            inviteView2.setOnClickListener(this);
//            inviteView3.setOnClickListener(this);


//            pushBtn = (TextView) findViewById(R.id.push_btn);
//            pushBtn.setVisibility(View.VISIBLE);
//            pushBtn.setOnClickListener(this);
//
//            recordBtn = (TextView) findViewById(R.id.record_btn);
//            recordBtn.setVisibility(View.VISIBLE);
//            recordBtn.setOnClickListener(this);

            initDetailDialog();
//            initPushDialog();
//            initRecordDialog();


//            mMemberDg = new MembersDialog(this, R.style.floag_dialog, this);
//            mMemberDg.setCanceledOnTouchOutside(true);
//            startRecordAnimation();
//            showHeadIcon(mHeadIcon, MySelfInfo.getInstance().getAvatar());
//            mBeautySettings = (LinearLayout) findViewById(R.id.qav_beauty_setting);
//            mBeautyConfirm = (TextView) findViewById(R.id.qav_beauty_setting_finish);
//            mBeautyConfirm.setOnClickListener(this);
//            mBeautyBar = (SeekBar) (findViewById(R.id.qav_beauty_progress));
//            mBeautyBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//
//                @Override
//                public void onStopTrackingTouch(SeekBar seekBar) {
//                    Log.d("SeekBar", "onStopTrackingTouch");
//                    if (mProfile == mBeatuy) {
//                        Toast.makeText(LiveActivity.this, "beauty " + mBeautyRate + "%", Toast.LENGTH_SHORT).show();//美颜度
//                    } else {
//                        Toast.makeText(LiveActivity.this, "white " + mWhiteRate + "%", Toast.LENGTH_SHORT).show();//美白度
//                    }
//                }
//
//                @Override
//                public void onStartTrackingTouch(SeekBar seekBar) {
//                    SxbLog.d("SeekBar", "onStartTrackingTouch");
//                }
//
//                @Override
//                public void onProgressChanged(SeekBar seekBar, int progress,
//                                              boolean fromUser) {
//                    Log.i(TAG, "onProgressChanged " + progress);
//                    if (mProfile == mBeatuy) {
//                        mBeautyRate = progress;
//                        QavsdkControl.getInstance().getAVContext().getVideoCtrl().inputBeautyParam(getBeautyProgress(progress));//美颜
//                    } else {
//                        mWhiteRate = progress;
//                        QavsdkControl.getInstance().getAVContext().getVideoCtrl().inputWhiteningParam(getBeautyProgress(progress));//美白
//                    }
//                }
//            });
        } else {
//            LinearLayout llRecordTip = (LinearLayout) findViewById(R.id.record_tip);
//            llRecordTip.setVisibility(View.GONE);
//            mHostNameTv.setVisibility(View.VISIBLE);
//            initInviteDialog();
//            mNomalMemberCtrView.setVisibility(View.VISIBLE);
//            mHostCtrView.setVisibility(View.GONE);
//            mVideoChat.setVisibility(View.GONE);
//            BtnScreen = (TextView) findViewById(R.id.clean_screen);
//
//            List<String> ids = new ArrayList<>();
//            ids.add(CurLiveInfo.getHostID());
//            showHeadIcon(mHeadIcon, CurLiveInfo.getHostAvator());
//            mHostNameTv.setText( CurLiveInfo.getHostName()  );
//
//            mHostLayout = (LinearLayout) findViewById(R.id.head_up_layout);
//            mHostLayout.setOnClickListener(this);
//            BtnScreen.setOnClickListener(this);
        }
        avView = findViewById(R.id.av_video_layer_ui);//surfaceView;
//        BtnBack = (TextView) findViewById(R.id.btn_back);
//        BtnBack.setOnClickListener(this);

//        mArrayListChatEntity = new ArrayList<ChatEntity>();

//        tvMembers.setText("" + CurLiveInfo.getMembers());
//        tvAdmires.setText("" + CurLiveInfo.getAdmires());


        mFullControllerUi = findViewById(R.id.control_ui);

        initVp();

    }

    private void initVp() {
        ViewPager vp = (ViewPager) findViewById(R.id.vp);
        vp.setAdapter(new LiveVpAdapter());
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    private TextView mDetailTime, mDetailAdmires, mDetailWatchCount;

    private void initDetailDialog() {
        mDetailDialog = new Dialog(this);
        mDetailDialog.setContentView(R.layout.dialog_live_detail);
        mDetailTime = (TextView) mDetailDialog.findViewById(R.id.tv_time);
        mDetailAdmires = (TextView) mDetailDialog.findViewById(R.id.tv_admires);
        mDetailWatchCount = (TextView) mDetailDialog.findViewById(R.id.tv_members);

        mDetailDialog.setCancelable(false);

        TextView tvCancel = (TextView) mDetailDialog.findViewById(R.id.btn_cancel);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDetailDialog.dismiss();
                finish();
            }
        });
//        mDetailDialog.show();
    }

    private Dialog backDialog;

    private void initBackDialog() {
        backDialog = new Dialog(this);
        backDialog.setContentView(R.layout.dialog_end_live);
        TextView tvSure = (TextView) backDialog.findViewById(R.id.btn_sure);
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mLiveHelper) {
                    mLiveHelper.perpareQuitRoom(true);
                    if (isPushed) {
                        mLiveHelper.stopPushAction();
                    }
                }
                backDialog.dismiss();
            }
        });
        TextView tvCancel = (TextView) backDialog.findViewById(R.id.btn_cancel);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backDialog.cancel();
            }
        });
//        backDialog.show();
    }

    /**
     * 点击Back键
     */
    @Override
    public void onBackPressed() {
        if (bInAvRoom) {
            bDelayQuit = false;
            quiteLiveByPurpose();
        } else {
            finish();
        }
    }

    /**
     * 当前用户主动退出直播
     */
    private void quiteLiveByPurpose() {
        if (MySelfInfo.getInstance().getIdStatus() == Constants.HOST) {
            if (backDialog.isShowing() == false) {
                backDialog.show();
            }
        } else {
            if (backDialog.isShowing() == false) {
                backDialog.show();
            }
            // 老师和学生都弹出退出对话框，学生不是直接退出
//            mLiveHelper.perpareQuitRoom(true);
//            // 下面这句重复
//            mEnterRoomHelper.quiteAVRoom();
        }
    }

    /**
     * 当前用户已经发完退出消息了
     */
    public void readyToQuit() {
        mEnterRoomHelper.quiteAVRoom();
    }

    /**
     * 被动退出直播（主播退出了房间，所以当前用户肯定时围观群众或者上麦DJ）
     */
    private void quiteLivePassively() {
//        Toast.makeText(this, "Host leave Live ", Toast.LENGTH_SHORT);
        mLiveHelper.perpareQuitRoom(false);
        // 下面这句重复
        mEnterRoomHelper.quiteAVRoom();
    }

//    private QavsdkControl.OnSlideListener mOnSlideListener = new QavsdkControl.OnSlideListener() {
//        @Override
//        public void onSlideUp() {
//            Log.e(TAG,"onSlideUp");
//        }
//
//        @Override
//        public void onSlideDown() {
//            Log.e(TAG,"onSlideDown");
//        }
//
//        @Override
//        public void onSlideLeft() {
//            Log.e(TAG,"onSlideLeft");
//        }
//
//        @Override
//        public void onSlideRight() {
//            Log.e(TAG,"onSlideRight");
//        }
//    };


    /**
     * 当前用户进入 av房间
     */
    public void enterRoomComplete(int id_status, boolean isSucc) {
//        Toast.makeText(LiveActivity.this, "EnterRoom  " + id_status + " isSucc " + isSucc, Toast.LENGTH_SHORT).show();
        //必须得进入房间之后才能初始化UI
        mEnterRoomHelper.initAvUILayer(avView);
//        QavsdkControl.getInstance().setSlideListener(mOnSlideListener);
        bInAvRoom = true;
        bDelayQuit = true;
        updateHostLeaveLayout();

        //设置预览回调，修正摄像头镜像
        mLiveHelper.setCameraPreviewChangeCallback();
        if (isSucc == true) {
            //IM初始化
            mLiveHelper.initTIMListener("" + CurLiveInfo.getRoomNum());

            if (id_status == Constants.HOST) {
                // 当前用户是主播
                Log.i(TAG, "createlive enterRoomComplete isSucc" + isSucc);
            } else {
                // 当前用户非主播 ， 进入房间， 发消息通知上线
                mLiveHelper.sendGroupMessage(Constants.AVIMCMD_EnterLive, "");
            }
            // 通知 业务服务器 当前用户 进入房间
            talkieLiveRoomJoin();
            // 上麦DJ
            if (TextUtils.equals(getIntent().getStringExtra("type"), "vip")) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 上麦
                        mLiveHelper.changeAuthandRole(true, Constants.VIDEO_MEMBER_AUTH, Constants.VIDEO_MEMBER_ROLE);
                    }
                }, 1000);
            }
        }

        bReadyToChange = false;
    }

    /**
     * 当前用户 退出 av房间 完成
     */
    public void quiteRoomComplete(int id_status, boolean succ, LiveInfoJson liveinfo) {
        if (MySelfInfo.getInstance().getIdStatus() == Constants.HOST) {
            // 当前用户是主播，显示本次直播数据
//            if ((getBaseContext() != null) && (null != mDetailDialog) && (mDetailDialog.isShowing() == false)) {
//                Log.d(TAG, "LogConstants.ACTION_HOST_QUIT_ROOM " + MySelfInfo.getInstance().getId() + "  " + "quite room callback" + "LogConstants.STATUS.SUCCEED" + "id status " + id_status);
//                mDetailTime.setText(formatTime);
//                mDetailAdmires.setText("" + CurLiveInfo.getAdmires());
//                mDetailWatchCount.setText("" + watchCount);
//                mDetailDialog.show();
//            }
            if (getBaseContext() != null) {
                new AlertDialog.Builder(getActivity())
                        .setTitle(getText(R.string.class_over))
                        .setPositiveButton(getText(R.string.evaluate), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                startActivity(new Intent().setClass(getActivity(), StudentListOfLessonActivity.class).putExtra(StudentListOfLessonActivity.EXTRA_LESSON, mLesson));
                                dialog.dismiss();
                                getActivity().finish();
                            }
                        })
                        .setNegativeButton(getText(R.string.back), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                getActivity().finish();
                            }
                        })
                        .setCancelable(false)
                        .show();
            }
        } else {
            // 当前用户是群众
            //finish();
            if (bInAvRoom) {
                if (bReadyToChange) {
                    clearOldData();
//                    mLiveListViewHelper.getPageData();
                }
            } else {
                bReadyToChange = false;
            }
//            if (bDelayQuit) { // bDelayQuit:true主播离开 被动退出 ； bDelayQuit:false 当前用户主动退出
//                // 此处好像是导致主播退出 群众还不会推出的bug原因
//                updateHostLeaveLayout();
//            } else {
//                finish();
//            }
            finish();

        }
        // 通知业务服务器用户退出房间
        if (MySelfInfo.getInstance().getIdStatus() == Constants.HOST) {
            // teacher
            talkieLiveRoomDismiss();
        } else if (MySelfInfo.getInstance().getIdStatus() == Constants.VIDEO_MEMBER) {
            // vip
            talkieLiveRoomLeave();
        } else {
            talkieLiveRoomLeave();
        }
        bInAvRoom = false;
    }


    /**
     * 其他用户（非当前用户）加入。来自自定义im消息
     */
    public void memberJoin(String id, String name) {
//        Log.d(TAG, LogConstants.ACTION_VIEWER_ENTER_ROOM + LogConstants.DIV + MySelfInfo.getInstance().getId() + LogConstants.DIV + "on member join" + LogConstants.DIV + "join room " + id);
        watchCount++;
//        refreshTextListView(TextUtils.isEmpty(name) ? id : name, "join live", Constants.MEMBER_ENTER);

        CurLiveInfo.setMembers(CurLiveInfo.getMembers() + 1);
//        tvMembers.setText("" + CurLiveInfo.getMembers());
    }

    /**
     * 其他用户（非当前用户）退出。来自自定义im消息
     */
    public void memberQuit(String id, String name) {
//        refreshTextListView(TextUtils.isEmpty(name) ? id : name, "quite live", Constants.MEMBER_EXIT);
        Log.e(TAG, "memberQuit:" + id);
        if (CurLiveInfo.getMembers() > 1) {
            CurLiveInfo.setMembers(CurLiveInfo.getMembers() - 1);
//            tvMembers.setText("" + CurLiveInfo.getMembers());
        }

        //如果存在视频互动，取消
        QavsdkControl.getInstance().closeMemberView(id);
    }

    /**
     * 主播暂时离开 ，当前用户非主播。来自自定义im消息
     */
    public void hostLeave(String id, String name) {
//        refreshTextListView(TextUtils.isEmpty(name) ? id : name, "leave for a while", Constants.HOST_LEAVE);
        Toast.makeText(getActivity(), getText(R.string.member_left_temporary), Toast.LENGTH_SHORT).show();
    }

    /**
     * 主播回来 ，当前用户非主播。来自自定义im消息
     */
    public void hostBack(String id, String name) {
//        refreshTextListView(TextUtils.isEmpty(name) ? id : name, "is back", Constants.HOST_BACK);
    }

    /**
     * 有人进房间。来自AVRoomMulti.EventListener 的回调 onEndpointsUpdateInfo
     */
    public void memberJoinLive(final String[] list) {
    }

    /**
     * 有人（非当前用户）退出房间。来自AVRoomMulti.EventListener 的回调 onEndpointsUpdateInfo
     */
    public void memberQuiteLive(String[] list) {
        if (list == null) return;
        for (String id : list) {
            Log.i(TAG, "memberQuiteLive id " + id); // im_10002701
            if (CurLiveInfo.getHostID().equals(id)) {
                if (MySelfInfo.getInstance().getIdStatus() == Constants.MEMBER) {
                    // 退出的人是主播，当前用户是围观群众
                    quiteLivePassively();
                } else if (MySelfInfo.getInstance().getIdStatus() == Constants.VIDEO_MEMBER) {
                    // 退出的人是主播，当前用户是上麦DJ
                    quiteLivePassively();
                }
            }
        }
    }


    public void alreadyInLive(String[] list) {
        for (String id : list) {
            if (id.equals(MySelfInfo.getInstance().getId())) {
                QavsdkControl.getInstance().setSelfId(MySelfInfo.getInstance().getId());
                QavsdkControl.getInstance().setLocalHasVideo(true, MySelfInfo.getInstance().getId());
            } else {
                QavsdkControl.getInstance().setRemoteHasVideo(true, id, AVView.VIDEO_SRC_TYPE_CAMERA);
            }
        }

    }

//    /**
//     * 红点动画
//     */
//    private void startRecordAnimation() {
//        mObjAnim = ObjectAnimator.ofFloat(mRecordBall, "alpha", 1f, 0f, 1f);
//        mObjAnim.setDuration(1000);
//        mObjAnim.setRepeatCount(-1);
//        mObjAnim.start();
//    }

    private static int index = 0;

    /**
     * 加载视频数据
     *
     * @param isLocal 是否是本地数据
     * @param id      身份
     */
    public void showVideoView(boolean isLocal, String id) {
        Log.i(TAG, "showVideoView " + id);
        //渲染本地Camera
        if (isLocal == true) {
            Log.i(TAG, "showVideoView host :" + MySelfInfo.getInstance().getId());
            QavsdkControl.getInstance().setSelfId(MySelfInfo.getInstance().getId());
            QavsdkControl.getInstance().setLocalHasVideo(true, MySelfInfo.getInstance().getId());

            if (bFirstRender) {
                if (MySelfInfo.getInstance().getIdStatus() == Constants.HOST) {
                    // 主播 通知 业务服务器 av房间创建完成
                    mEnterRoomHelper.notifyServerCreateRoom();

//                    //主播心跳
//                    mHearBeatTimer = new Timer(true);
//                    mHeartBeatTask = new HeartBeatTask();
//                    mHearBeatTimer.schedule(mHeartBeatTask, 1000, 3 * 1000);
//
                    //直播时间
                    mVideoTimer = new Timer(true);
                    VideoTimerTask mVideoTimerTask = new VideoTimerTask();
                    mVideoTimer.schedule(mVideoTimerTask, 1000, 1000 * 60);

                    // 课程人数 等状态
                    mLessonStatusTimer = new Timer(true);
                    LessonStatusTimerTask mLessonStatusTimerTask = new LessonStatusTimerTask();
                    mLessonStatusTimer.schedule(mLessonStatusTimerTask, 1000, 1000 * 5);
                }
                bFirstRender = false;
            }
        } else {
//            QavsdkControl.getInstance().addRemoteVideoMembers(id);
            if (isScreenShare) {
                QavsdkControl.getInstance().setRemoteHasVideo(true, id, AVView.VIDEO_SRC_TYPE_SCREEN);
                isScreenShare = false;
            } else {
                QavsdkControl.getInstance().setRemoteHasVideo(true, id, AVView.VIDEO_SRC_TYPE_CAMERA);
            }
        }

    }


//    private float getBeautyProgress(int progress) {
//        Log.d("shixu", "progress: " + progress);
//        return (9.0f * progress / 100.0f);
//    }


    public void showInviteDialog() {
//        if ((inviteDg != null) && (getBaseContext() != null) && (inviteDg.isShowing() != true)) {
//            inviteDg.show();
//        }
    }

    public void hideInviteDialog() {
//        if ((inviteDg != null) && (inviteDg.isShowing() == true)) {
//            inviteDg.dismiss();
//        }
    }


    public void refreshText(String text, String name) {
        if (text != null) {
//            refreshTextListView(name, text, Constants.TEXT_TYPE);
        }
    }

    public void refreshUI(String id) {
        //当主播选中这个人，而他主动/被动退出时需要恢复到正常状态
        if (MySelfInfo.getInstance().getIdStatus() == Constants.HOST) {
            if (!backGroundId.equals(CurLiveInfo.getHostID()) && backGroundId.equals(id)) {
                backToNormalCtrlView();
            }
        }
    }


    private int inviteViewCount = 0;

    /**
     * 当前用户是主播，邀请其他人上麦，从打开围观群众列表选中一个人后 相关操作
     *
     * @param id 被邀请用户的id
     * @return 邀请成功
     */
    public boolean showInviteView(String id) {
        Log.d(TAG, "LogConstants.ACTION_VIEWER_SHOW：self " + MySelfInfo.getInstance().getId() + "  invite up show ； " + "id ：" + id);
        int index = QavsdkControl.getInstance().getAvailableViewIndex(1);
        if (index == -1) {
            Toast.makeText(LiveActivity.this, "the invitation's upper limit is 3", Toast.LENGTH_SHORT).show();
            return false;
        }
        int requetCount = index + inviteViewCount;
        if (requetCount > 3) {
            Toast.makeText(LiveActivity.this, "the invitation's upper limit is 3", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (hasInvited(id)) {
            Toast.makeText(LiveActivity.this, "it has already invited", Toast.LENGTH_SHORT).show();
            return false;
        }
        switch (requetCount) {
            case 1:
                inviteView1.setText(id);
                inviteView1.setVisibility(View.VISIBLE);
                inviteView1.setTag(id);
                break;
            case 2:
                inviteView2.setText(id);
                inviteView2.setVisibility(View.VISIBLE);
                inviteView2.setTag(id);
                break;
            case 3:
                inviteView3.setText(id);
                inviteView3.setVisibility(View.VISIBLE);
                inviteView3.setTag(id);
                break;
        }
        // 发出邀请请求
        mLiveHelper.sendC2CMessage(Constants.AVIMCMD_MUlTI_HOST_INVITE, "", id);
        inviteViewCount++;
        //30s超时取消
        Message msg = new Message();
        msg.what = TIMEOUT_INVITE;
        msg.obj = id;
        mHandler.sendMessageDelayed(msg, 30 * 1000);
        return true;
    }


    /**
     * 判断是否邀请过同一个人
     *
     * @param id
     * @return
     */
    private boolean hasInvited(String id) {
        if (id.equals(inviteView1.getTag())) {
            return true;
        }
        if (id.equals(inviteView2.getTag())) {
            return true;
        }
        if (id.equals(inviteView3.getTag())) {
            return true;
        }
        return false;
    }

    public void cancelInviteView(String id) {
        if ((inviteView1 != null) && (inviteView1.getTag() != null)) {
            if (inviteView1.getTag().equals(id)) {
            }
            if (inviteView1.getVisibility() == View.VISIBLE) {
                inviteView1.setVisibility(View.INVISIBLE);
                inviteView1.setTag("");
                inviteViewCount--;
            }
        }

        if (inviteView2 != null && inviteView2.getTag() != null) {
            if (inviteView2.getTag().equals(id)) {
                if (inviteView2.getVisibility() == View.VISIBLE) {
                    inviteView2.setVisibility(View.INVISIBLE);
                    inviteView2.setTag("");
                    inviteViewCount--;
                }
            } else {
                Log.i(TAG, "cancelInviteView inviteView2 is null");
            }
        } else {
            Log.i(TAG, "cancelInviteView inviteView2 is null");
        }

        if (inviteView3 != null && inviteView3.getTag() != null) {
            if (inviteView3.getTag().equals(id)) {
                if (inviteView3.getVisibility() == View.VISIBLE) {
                    inviteView3.setVisibility(View.INVISIBLE);
                    inviteView3.setTag("");
                    inviteViewCount--;
                }
            } else {
                Log.i(TAG, "cancelInviteView inviteView3 is null");
            }
        } else {
            Log.i(TAG, "cancelInviteView inviteView3 is null");
        }


    }

    public void cancelMemberView(String id) {
        if (MySelfInfo.getInstance().getIdStatus() == Constants.HOST) {
        } else {
            //TODO 主动下麦 下麦；
//            Log.d(TAG, LogConstants.ACTION_VIEWER_UNSHOW + LogConstants.DIV + MySelfInfo.getInstance().getId() + LogConstants.DIV + "start unShow" +
//                    LogConstants.DIV + "id " + id);
            mLiveHelper.changeAuthandRole(false, Constants.NORMAL_MEMBER_AUTH, Constants.NORMAL_MEMBER_ROLE);
//            mLiveHelper.closeCameraAndMic();//是自己成员关闭
        }
        mLiveHelper.sendGroupMessage(Constants.AVIMCMD_MULTI_CANCEL_INTERACT, id);
        QavsdkControl.getInstance().closeMemberView(id);
        backToNormalCtrlView();
    }


    private void showReportDialog() {
//        final Dialog reportDialog = new Dialog(this, R.style.report_dlg);
//        reportDialog.setContentView(R.layout.dialog_live_report);
//
//        TextView tvReportDirty = (TextView) reportDialog.findViewById(R.id.btn_dirty);
//        TextView tvReportFalse = (TextView) reportDialog.findViewById(R.id.btn_false);
//        TextView tvReportVirus = (TextView) reportDialog.findViewById(R.id.btn_virus);
//        TextView tvReportIllegal = (TextView) reportDialog.findViewById(R.id.btn_illegal);
//        TextView tvReportYellow = (TextView) reportDialog.findViewById(R.id.btn_yellow);
//        TextView tvReportCancel = (TextView) reportDialog.findViewById(R.id.btn_cancel);
//
//        View.OnClickListener listener = new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                switch (v.getId()) {
//                    default:
//                        reportDialog.cancel();
//                        break;
//                }
//            }
//        };
//
//        tvReportDirty.setOnClickListener(listener);
//        tvReportFalse.setOnClickListener(listener);
//        tvReportVirus.setOnClickListener(listener);
//        tvReportIllegal.setOnClickListener(listener);
//        tvReportYellow.setOnClickListener(listener);
//        tvReportCancel.setOnClickListener(listener);
//
//        reportDialog.setCanceledOnTouchOutside(true);
//        reportDialog.show();
    }

    private void showHostDetail() {
//        Dialog hostDlg = new Dialog(this, R.style.host_info_dlg);
//        hostDlg.setContentView(R.layout.host_info_layout);
//
//        WindowManager windowManager = getWindowManager();
//        Display display = windowManager.getDefaultDisplay();
//        Window dlgwin = hostDlg.getWindow();
//        WindowManager.LayoutParams lp = dlgwin.getAttributes();
//        dlgwin.setGravity(Gravity.TOP);
//        lp.width = (int) (display.getWidth()); //设置宽度
//
//        hostDlg.getWindow().setAttributes(lp);
//        hostDlg.show();
//
//        TextView tvHost = (TextView) hostDlg.findViewById(R.id.tv_host_name);
//        tvHost.setText(CurLiveInfo.getHostName());
//        ImageView ivHostIcon = (ImageView) hostDlg.findViewById(R.id.iv_host_icon);
//        showHeadIcon(ivHostIcon, CurLiveInfo.getHostAvator());
//        TextView tvLbs = (TextView) hostDlg.findViewById(R.id.tv_host_lbs);
//        tvLbs.setText(UIUtils.getLimitString(CurLiveInfo.getAddress(), 6));
//        ImageView ivReport = (ImageView) hostDlg.findViewById(R.id.iv_report);
//        ivReport.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                showReportDialog();
//            }
//        });
    }

    private boolean checkInterval() {
        if (0 == admireTime) {
            admireTime = System.currentTimeMillis();
            return true;
        }
        long newTime = System.currentTimeMillis();
        if (newTime >= admireTime + 1000) {
            admireTime = newTime;
            return true;
        }
        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
//            case R.id.btn_back:
//                quiteLiveByPurpose();
//                onBackPressed();
//                break;
//            case R.id.message_input:
//                inputMsgDialog();
//                break;
//            case R.id.member_send_good:
//                // 添加飘星动画
//                mHeartLayout.addFavor();
//                if (checkInterval()) {
//                    //mLiveHelper.sendC2CMessage(Constants.AVIMCMD_Praise, "", CurLiveInfo.getHostID());
//                    mLiveHelper.sendGroupMessage(Constants.AVIMCMD_Praise, "");
//                    CurLiveInfo.setAdmires(CurLiveInfo.getAdmires() + 1);
//                    tvAdmires.setText("" + CurLiveInfo.getAdmires());
//                } else {
//                    //Toast.makeText(this, getString(R.string.text_live_admire_limit), Toast.LENGTH_SHORT).show();
//                }
//                break;
//            case R.id.flash_btn:
//                if (mLiveHelper.isFrontCamera() == true) {
//                    Toast.makeText(LiveActivity.this, "this is front cam", Toast.LENGTH_SHORT).show();
//                } else {
//                    mLiveHelper.toggleFlashLight();
//                }
//                break;
//            case R.id.switch_cam:
//                mLiveHelper.switchCamera();
//                break;
//            case R.id.mic_btn:
//                if (mLiveHelper.isMicOpen() == true) {
//                    BtnMic.setBackgroundResource(R.drawable.icon_mic_close);
//                    mLiveHelper.muteMic();
//                } else {
//                    BtnMic.setBackgroundResource(R.drawable.icon_mic_open);
//                    mLiveHelper.openMic();
//                }
//                break;
//            case R.id.head_up_layout:
//                showHostDetail();
//                break;
//            case R.id.video_interact:
//                mMemberDg.setCanceledOnTouchOutside(true);
//                mMemberDg.show();
//                break;
//            case R.id.camera_controll:
//                Toast.makeText(LiveActivity.this, "切换" + backGroundId + "camrea 状态", Toast.LENGTH_SHORT).show();
//                if (backGroundId.equals(MySelfInfo.getInstance().getId())) {//自己关闭自己
//                    mLiveHelper.toggleCamera();
//                } else {
//                    mLiveHelper.sendC2CMessage(Constants.AVIMCMD_MULTI_HOST_CONTROLL_CAMERA, backGroundId, backGroundId);//主播关闭自己
//                }
//                break;
//            case R.id.mic_controll:
//                Toast.makeText(LiveActivity.this, "切换" + backGroundId + "mic 状态", Toast.LENGTH_SHORT).show();
//                if (backGroundId.equals(MySelfInfo.getInstance().getId())) {//自己关闭自己
//                    mLiveHelper.toggleMic();
//                } else {
//                    mLiveHelper.sendC2CMessage(Constants.AVIMCMD_MULTI_HOST_CONTROLL_MIC, backGroundId, backGroundId);//主播关闭自己
//                }
//                break;
//            case R.id.close_member_video://主动关闭成员摄像头
//                cancelMemberView(backGroundId);
//                break;
//            case R.id.beauty_btn:
//                Log.i(TAG, "onClick " + mBeautyRate);
//
//                mProfile = mBeatuy;
//                if (mBeautySettings != null) {
//                    if (mBeautySettings.getVisibility() == View.GONE) {
//                        mBeautySettings.setVisibility(View.VISIBLE);
//                        mFullControllerUi.setVisibility(View.INVISIBLE);
//                        mBeautyBar.setProgress(mBeautyRate);
//                    } else {
//                        mBeautySettings.setVisibility(View.GONE);
//                        mFullControllerUi.setVisibility(View.VISIBLE);
//                    }
//                } else {
//                    Log.i(TAG, "beauty_btn mTopBar  is null ");
//                }
//                break;
//
//            case R.id.white_btn:
//                Log.i(TAG, "onClick " + mWhiteRate);
//                mProfile = mWhite;
//                if (mBeautySettings != null) {
//                    if (mBeautySettings.getVisibility() == View.GONE) {
//                        mBeautySettings.setVisibility(View.VISIBLE);
//                        mFullControllerUi.setVisibility(View.INVISIBLE);
//                        mBeautyBar.setProgress(mWhiteRate);
//                    } else {
//                        mBeautySettings.setVisibility(View.GONE);
//                        mFullControllerUi.setVisibility(View.VISIBLE);
//                    }
//                } else {
//                    Log.i(TAG, "beauty_btn mTopBar  is null ");
//                }
//                break;
//            case R.id.qav_beauty_setting_finish:
//                mBeautySettings.setVisibility(View.GONE);
//                mFullControllerUi.setVisibility(View.VISIBLE);
//                break;
            case R.id.invite_view1:
                inviteView1.setVisibility(View.INVISIBLE);
                inviteView1.setTag("");
                mLiveHelper.sendGroupMessage(Constants.AVIMCMD_MULTI_CANCEL_INTERACT, "" + inviteView1.getTag());
                break;
            case R.id.invite_view2:
                inviteView2.setVisibility(View.INVISIBLE);
                inviteView2.setTag("");
                mLiveHelper.sendGroupMessage(Constants.AVIMCMD_MULTI_CANCEL_INTERACT, "" + inviteView2.getTag());
                break;
            case R.id.invite_view3:
                inviteView3.setVisibility(View.INVISIBLE);
                inviteView3.setTag("");
                mLiveHelper.sendGroupMessage(Constants.AVIMCMD_MULTI_CANCEL_INTERACT, "" + inviteView3.getTag());
                break;
//            case R.id.param_video:
//                showTips = !showTips;
//                break;
//            case R.id.push_btn:
//                pushStream();
//                break;
//            case R.id.record_btn:
//                if (!mRecord) {
//                    if (recordDialog != null)
//                        recordDialog.show();
//                } else {
//                    mLiveHelper.stopRecord();
//                }
//                break;
//            case R.id.speed_test_btn:
//                new SpeedTestDialog(this).start();
//                break;

        }
    }

    //for 测试获取测试参数
    private boolean showTips = false;
    private TextView tvTipsMsg;
    Timer paramTimer = new Timer();
    TimerTask task = new TimerTask() {
        public void run() {
            runOnUiThread(new Runnable() {
                public void run() {
                    if (showTips) {
                        if (tvTipsMsg != null) {
                            String strTips = QavsdkControl.getInstance().getQualityTips();
                            strTips = praseString(strTips);
                            if (!TextUtils.isEmpty(strTips)) {
                                tvTipsMsg.setText(strTips);
                            }
                        }
                    } else {
                        tvTipsMsg.setText("");
                    }
                }
            });
        }
    };

    //for 测试 解析参数
    private String praseString(String video) {
        if (video.length() == 0) {
            return "";
        }
        String result = "";
        String splitItems[];
        String tokens[];
        splitItems = video.split("\\n");
        for (int i = 0; i < splitItems.length; ++i) {
            if (splitItems[i].length() < 2)
                continue;

            tokens = splitItems[i].split(":");
            if (tokens[0].length() == "mainVideoSendSmallViewQua".length()) {
                continue;
            }
            if (tokens[0].endsWith("BigViewQua")) {
                tokens[0] = "mainVideoSendViewQua";
            }
            if (tokens[0].endsWith("BigViewQos")) {
                tokens[0] = "mainVideoSendViewQos";
            }
            result += tokens[0] + ":\n" + "\t\t";
            for (int j = 1; j < tokens.length; ++j)
                result += tokens[j];
            result += "\n\n";
            //Log.d(TAG, "test:" + result);
        }
        //Log.d(TAG, "test:" + result);
        return result;
    }


    private void backToNormalCtrlView() {
        if (MySelfInfo.getInstance().getIdStatus() == Constants.HOST) {
            backGroundId = CurLiveInfo.getHostID();
//            mHostCtrView.setVisibility(View.VISIBLE);
//            mVideoMemberCtrlView.setVisibility(View.GONE);
        } else {
            backGroundId = CurLiveInfo.getHostID();
//            mNomalMemberCtrView.setVisibility(View.VISIBLE);
//            mVideoMemberCtrlView.setVisibility(View.GONE);
        }
    }


    /**
     * 发消息弹出框
     */
//    private void inputMsgDialog() {
//        InputTextMsgDialog inputMsgDialog = new InputTextMsgDialog(this, R.style.inputdialog, mLiveHelper, this);
//        WindowManager windowManager = getWindowManager();
//        Display display = windowManager.getDefaultDisplay();
//        WindowManager.LayoutParams lp = inputMsgDialog.getWindow().getAttributes();
//
//        lp.width = (int) (display.getWidth()); //设置宽度
//        inputMsgDialog.getWindow().setAttributes(lp);
//        inputMsgDialog.setCancelable(true);
//        inputMsgDialog.show();
//    }


    /**
     * 围观群众 接收到了 主播DJ的上麦邀请 的 应答框
     */
//    private void initInviteDialog() {
//        inviteDg = new Dialog(this, R.style.dialog);
//        inviteDg.setContentView(R.layout.invite_dialog);
//        TextView hostId = (TextView) inviteDg.findViewById(R.id.host_id);
//        hostId.setText(CurLiveInfo.getHostID());
//        TextView agreeBtn = (TextView) inviteDg.findViewById(R.id.invite_agree);
//        TextView refusebtn = (TextView) inviteDg.findViewById(R.id.invite_refuse);
//        agreeBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
////                mVideoMemberCtrlView.setVisibility(View.VISIBLE);
////                mNomalMemberCtrView.setVisibility(View.INVISIBLE);
////                Log.d(TAG, LogConstants.ACTION_VIEWER_SHOW + LogConstants.DIV + MySelfInfo.getInstance().getId() + LogConstants.DIV + "accept invite" + LogConstants.DIV + "host id " + CurLiveInfo.getHostID());
//                //上麦
//                mLiveHelper.changeAuthandRole(true, Constants.VIDEO_MEMBER_AUTH, Constants.VIDEO_MEMBER_ROLE);
//                inviteDg.dismiss();
//            }
//        });
//
//        refusebtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mLiveHelper.sendC2CMessage(Constants.AVIMCMD_MUlTI_REFUSE, "", CurLiveInfo.getHostID());
//                inviteDg.dismiss();
//            }
//        });
//
//        Window dialogWindow = inviteDg.getWindow();
//        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//        dialogWindow.setGravity(Gravity.CENTER);
//        dialogWindow.setAttributes(lp);
//    }


//    /**
//     * 聊天消息刷新显示
//     *
//     * @param name    发送者
//     * @param context 内容
//     * @param type    类型 （上线线消息和 聊天消息）
//     */
//    public void refreshTextListView(String name, String context, int type) {
//        ChatEntity entity = new ChatEntity();
//        entity.setSenderName(name);
//        entity.setContext(context);
//        entity.setType(type);
//        //mArrayListChatEntity.add(entity);
//        notifyRefreshListView(entity);
//        //mChatMsgListAdapter.notifyDataSetChanged();
//
//        mListViewMsgItems.setVisibility(View.VISIBLE);
//        SxbLog.d(TAG, "refreshTextListView height " + mListViewMsgItems.getHeight());
//
//        if (mListViewMsgItems.getCount() > 1) {
//            if (true)
//                mListViewMsgItems.setSelection(0);
//            else
//                mListViewMsgItems.setSelection(mListViewMsgItems.getCount() - 1);
//        }
//
//    }


//    /**
//     * 通知刷新消息ListView
//     */
//    private void notifyRefreshListView(ChatEntity entity) {
//        mBoolNeedRefresh = true;
//        mTmpChatList.add(entity);
//        if (mBoolRefreshLock) {
//            return;
//        } else {
//            doRefreshListView();
//        }
//    }


//    /**
//     * 刷新ListView并重置状态
//     */
//    private void doRefreshListView() {
//        if (mBoolNeedRefresh) {
//            mBoolRefreshLock = true;
//            mBoolNeedRefresh = false;
//            mArrayListChatEntity.addAll(mTmpChatList);
//            mTmpChatList.clear();
////            mChatMsgListAdapter.notifyDataSetChanged();
//
//            if (null != mTimerTask) {
//                mTimerTask.cancel();
//            }
//            mTimerTask = new TimerTask() {
//                @Override
//                public void run() {
//                    Log.v(TAG, "doRefreshListView->task enter with need:" + mBoolNeedRefresh);
//                    mHandler.sendEmptyMessage(REFRESH_LISTVIEW);
//                }
//            };
//            //mTimer.cancel();
//            mTimer.schedule(mTimerTask, MINFRESHINTERVAL);
//        } else {
//            mBoolRefreshLock = false;
//        }
//    }
    public void updateProfileInfo(TIMUserProfile profile) {

    }

//    public void updateUserInfo(int requestCode, List<TIMUserProfile> profiles) {
//        if (null != profiles) {
//            switch (requestCode) {
//                case GETPROFILE_JOIN:
//                    for (TIMUserProfile user : profiles) {
////                        tvMembers.setText("" + CurLiveInfo.getMembers());
//                        Log.w(TAG, "get nick name:" + user.getNickName());
//                        Log.w(TAG, "get remark name:" + user.getRemark());
//                        Log.w(TAG, "get avatar:" + user.getFaceUrl());
//                        if (!TextUtils.isEmpty(user.getNickName())) {
//                            refreshTextListView(user.getNickName(), "join live", Constants.MEMBER_ENTER);
//                        } else {
//                            refreshTextListView(user.getIdentifier(), "join live", Constants.MEMBER_ENTER);
//                        }
//                    }
//                    break;
//            }
//
//        }
//    }

    //旁路直播
    private static boolean isPushed = false;

//    /**
//     * 旁路直播 退出房间时必须退出推流。否则会占用后台channel。
//     */
//    public void pushStream() {
//        if (!isPushed) {
//            if (mPushDialog != null)
//                mPushDialog.show();
//        } else {
//            mLiveHelper.stopPushAction();
//        }
//    }

//    private Dialog mPushDialog;

//    private void initPushDialog() {
//        mPushDialog = new Dialog(this, R.style.dialog);
//        mPushDialog.setContentView(R.layout.push_dialog_layout);
//        final TIMAvManager.StreamParam mStreamParam = TIMAvManager.getInstance().new StreamParam();
//        final EditText pushfileNameInput = (EditText) mPushDialog.findViewById(R.id.push_filename);
//        final RadioGroup radgroup = (RadioGroup) mPushDialog.findViewById(R.id.push_type);
//
//
//        Button recordOk = (Button) mPushDialog.findViewById(R.id.btn_record_ok);
//        recordOk.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (pushfileNameInput.getText().toString().equals("")) {
//                    Toast.makeText(LiveActivity.this, "name can't be empty", Toast.LENGTH_SHORT);
//                    return;
//                } else {
//                    mStreamParam.setChannelName(pushfileNameInput.getText().toString());
//                }
//
//                if (radgroup.getCheckedRadioButtonId() == R.id.hls) {
//                    mStreamParam.setEncode(TIMAvManager.StreamEncode.HLS);
//                } else {
//                    mStreamParam.setEncode(TIMAvManager.StreamEncode.RTMP);
//                }
////                mStreamParam.setEncode(TIMAvManager.StreamEncode.HLS);
//                Log.d(TAG, LogConstants.ACTION_HOST_CREATE_ROOM + LogConstants.DIV + MySelfInfo.getInstance().getId() + LogConstants.DIV + "start push stream"
//                        + LogConstants.DIV + "room id " + MySelfInfo.getInstance().getMyRoomNum());
//                mLiveHelper.pushAction(mStreamParam);
//                mPushDialog.dismiss();
//            }
//        });
//
//
//        Button recordCancel = (Button) mPushDialog.findViewById(R.id.btn_record_cancel);
//        recordCancel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mPushDialog.dismiss();
//            }
//        });
//
//        Window dialogWindow = mPushDialog.getWindow();
//        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//        dialogWindow.setGravity(Gravity.CENTER);
//        dialogWindow.setAttributes(lp);
//        mPushDialog.setCanceledOnTouchOutside(false);
//    }


    /**
     * 推流成功
     *
     * @param streamRes
     */
    public void pushStreamSucc(TIMAvManager.StreamRes streamRes) {
//        List<TIMAvManager.LiveUrl> liveUrls = streamRes.getUrls();
        isPushed = true;
//        pushBtn.setText(R.string.live_btn_stop_push);
//        int length = liveUrls.size();
//        String url = null;
//        String url2 = null;
//        if (length == 1) {
//            TIMAvManager.LiveUrl avUrl = liveUrls.get(0);
//            url = avUrl.getUrl();
//        } else if (length == 2) {
//            TIMAvManager.LiveUrl avUrl = liveUrls.get(0);
//            url = avUrl.getUrl();
//            TIMAvManager.LiveUrl avUrl2 = liveUrls.get(1);
//            url2 = avUrl2.getUrl();
//        }
//        ClipToBoard(url, url2);
    }

    /**
     * 将地址黏贴到黏贴版
     *
     * @param url
     * @param url2
     */
//    private void ClipToBoard(final String url, final String url2) {
//        Log.i(TAG, "ClipToBoard url " + url);
//        Log.i(TAG, "ClipToBoard url2 " + url2);
//        if (url == null) return;
//        final Dialog dialog = new Dialog(this, R.style.dialog);
//        dialog.setContentView(R.layout.clip_dialog);
//        TextView urlText = ((TextView) dialog.findViewById(R.id.url1));
//        TextView urlText2 = ((TextView) dialog.findViewById(R.id.url2));
//        Button btnClose = ((Button) dialog.findViewById(R.id.close_dialog));
//        urlText.setText(url);
//        urlText.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                ClipboardManager clip = (ClipboardManager) getApplicationContext().getSystemService(getApplicationContext().CLIPBOARD_SERVICE);
//                clip.setText(url);
//                Toast.makeText(LiveActivity.this, getResources().getString(R.string.clip_tip), Toast.LENGTH_SHORT).show();
//            }
//        });
//        if (url2 == null) {
//            urlText2.setVisibility(View.GONE);
//        } else {
//            urlText2.setText(url2);
//            urlText2.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    ClipboardManager clip = (ClipboardManager) getApplicationContext().getSystemService(getApplicationContext().CLIPBOARD_SERVICE);
//                    clip.setText(url2);
//                    Toast.makeText(LiveActivity.this, getResources().getString(R.string.clip_tip), Toast.LENGTH_SHORT).show();
//                }
//            });
//        }
//        btnClose.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                dialog.dismiss();
//            }
//        });
//        dialog.setCanceledOnTouchOutside(false);
//        dialog.show();
//
//    }


    private Dialog recordDialog;
    private TIMAvManager.RecordParam mRecordParam;
    private String filename = "";
    private String tags = "";
    private String classId = "";
    private boolean mRecord = false;
    private EditText filenameEditText, tagEditText, classEditText;
    private CheckBox trancodeCheckBox, screenshotCheckBox, watermarkCheckBox, audioCheckBox;

    private void initRecordDialog() {
//        recordDialog = new Dialog(this, R.style.dialog);
//        recordDialog.setContentView(R.layout.record_param);
//        mRecordParam = TIMAvManager.getInstance().new RecordParam();
//
//        filenameEditText = (EditText) recordDialog.findViewById(R.id.record_filename);
//        tagEditText = (EditText) recordDialog.findViewById(R.id.record_tag);
//        classEditText = (EditText) recordDialog.findViewById(R.id.record_class);
//        trancodeCheckBox = (CheckBox) recordDialog.findViewById(R.id.record_tran_code);
//        screenshotCheckBox = (CheckBox) recordDialog.findViewById(R.id.record_screen_shot);
//        watermarkCheckBox = (CheckBox) recordDialog.findViewById(R.id.record_water_mark);
//        audioCheckBox = (CheckBox) recordDialog.findViewById(R.id.record_audio);
//
//        if (filename.length() > 0) {
//            filenameEditText.setText(filename);
//        }
//        filenameEditText.setText("" + CurLiveInfo.getRoomNum());
//
//        if (tags.length() > 0) {
//            tagEditText.setText(tags);
//        }
//
//        if (classId.length() > 0) {
//            classEditText.setText(classId);
//        }
//        Button recordOk = (Button) recordDialog.findViewById(R.id.btn_record_ok);
//        recordOk.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
////                Log.d(TAG, LogConstants.ACTION_HOST_CREATE_ROOM + LogConstants.DIV + MySelfInfo.getInstance().getId() + LogConstants.DIV + "start record"  + LogConstants.DIV + "room id " + MySelfInfo.getInstance().getMyRoomNum());
//                filename = filenameEditText.getText().toString();
//                mRecordParam.setFilename(filename);
//                tags = tagEditText.getText().toString();
//                classId = classEditText.getText().toString();
//                Log.d(TAG, "onClick classId " + classId);
//                if (classId.equals("")) {
//                    Toast.makeText(getApplicationContext(), "classID can not be empty", Toast.LENGTH_LONG).show();
//                    return;
//                }
//                mRecordParam.setClassId(Integer.parseInt(classId));
//                mRecordParam.setTransCode(trancodeCheckBox.isChecked());
//                mRecordParam.setSreenShot(screenshotCheckBox.isChecked());
//                mRecordParam.setWaterMark(watermarkCheckBox.isChecked());
//
//                if (audioCheckBox.isChecked()) {
//                    mRecordParam.setRecordType(TIMAvManager.RecordType.AUDIO);
//                } else {
//                    mRecordParam.setRecordType(TIMAvManager.RecordType.VIDEO);
//                }
//                mLiveHelper.startRecord(mRecordParam);
//                recordDialog.dismiss();
//            }
//        });
//        Button recordCancel = (Button) recordDialog.findViewById(R.id.btn_record_cancel);
//        recordCancel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                recordDialog.dismiss();
//            }
//        });
//        Window dialogWindow = recordDialog.getWindow();
//        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//        dialogWindow.setGravity(Gravity.CENTER);
//        dialogWindow.setAttributes(lp);
//        recordDialog.setCanceledOnTouchOutside(false);
    }

    /**
     * 停止推流成功
     */
    public void stopStreamSucc() {
        isPushed = false;
//        pushBtn.setText(R.string.live_btn_push);
    }

    public void startRecordCallback(boolean isSucc) {
        mRecord = true;
//        recordBtn.setText(R.string.live_btn_stop_record);
    }

    public void stopRecordCallback(boolean isSucc, List<String> files) {
        if (isSucc == true) {
            mRecord = false;
//            recordBtn.setText(R.string.live_btn_record);
        }
    }


    private VideoOrientationEventListener mOrientationEventListener;

    void registerOrientationListener() {
        if (mOrientationEventListener == null) {
            mOrientationEventListener = new VideoOrientationEventListener(super.getApplicationContext(), SensorManager.SENSOR_DELAY_UI);
        }
    }

    void startOrientationListener() {
        if (mOrientationEventListener != null) {
            mOrientationEventListener.enable();
        }
    }

    void stopOrientationListener() {
        if (mOrientationEventListener != null) {
            mOrientationEventListener.disable();
        }
    }


    class VideoOrientationEventListener extends OrientationEventListener {
        boolean mbIsTablet = true;

        public VideoOrientationEventListener(Context context, int rate) {
            super(context, rate);
            mbIsTablet = PhoneStatusTools.isTablet(context);
        }

        int mLastOrientation = -25;

        @Override
        public void onOrientationChanged(int orientation) {
            if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {      // 平放
                mLastOrientation = orientation;
                return;
            }

            if (mLastOrientation < 0) {
                mLastOrientation = 0;
            }

            if (((orientation - mLastOrientation) < 20)
                    && ((orientation - mLastOrientation) > -20)) {
                return;
            }

            //只检测是否有四个角度的改变
            if (QavsdkControl.getInstance() != null) {
                if (orientation > 350 || orientation < 10) { //0度
                    QavsdkControl.getInstance().setRotation(0);
                } else if (orientation > 80 && orientation < 100) { //90度
                    QavsdkControl.getInstance().setRotation(90);
                } else if (orientation > 170 && orientation < 190) { //180度
                    QavsdkControl.getInstance().setRotation(180);
                } else if (orientation > 260 && orientation < 280) { //270度
                    QavsdkControl.getInstance().setRotation(270);
                }
                return;
            }
        }
    }


    void checkPermission() {
        final List<String> permissionsList = new ArrayList<>();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if ((checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.CAMERA);
            if ((checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.RECORD_AUDIO);
            if ((checkSelfPermission(Manifest.permission.WAKE_LOCK) != PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.WAKE_LOCK);
            if ((checkSelfPermission(Manifest.permission.MODIFY_AUDIO_SETTINGS) != PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.MODIFY_AUDIO_SETTINGS);
            if (permissionsList.size() != 0) {
                requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), REQUEST_PHONE_PERMISSIONS);
            }
        }
    }

    /**
     * 离开房间前，清除房间数据
     */
    private void clearOldData() {
//        mArrayListChatEntity.clear();
        mBoolNeedRefresh = true;
        if (mBoolRefreshLock) {
            return;
        } else {
//            doRefreshListView();
        }
        QavsdkControl.getInstance().clearVideoData();
    }


    private void talkieLiveRoomJoin() {
        // 如果是vip或者teacher则通知 业务服务器
        if (!TextUtils.equals(getIntent().getStringExtra("type"), "vip") && !TextUtils.equals(getIntent().getStringExtra("type"), "teacher")) {
            return;
        }
//        // loading dialog
//        final LoadingDialogFragment loadingDialogFragment = new LoadingDialogFragment();
//        loadingDialogFragment.show(getSupportFragmentManager(), "loading");

        Log.e(TAG, "join :" + TalkieApi.getHost() + TalkieApi.PATH_LESSON + mLesson.id + "/join");

        OkHttpManager.client.newCall(
                new Request.Builder().url(TalkieApi.getHost() + TalkieApi.PATH_LESSON + mLesson.id + "/join")
                        //
                        .post(RequestBody.create(OkHttpManager.JSON, "{}"))
                        .build())
                //
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getActivity(), getText(R.string.network_exception), Toast.LENGTH_LONG).show();
                                // loading dialog
//                                loadingDialogFragment.dismiss();
                            }
                        });
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (!response.isSuccessful()) {
                            return;
                        }
                        String jsonStr = response.body().string();
                        Log.e(TAG, "join resp:" + jsonStr);

                        try {
                            final BaseResponse resp = new Gson().fromJson(jsonStr, BaseResponse.class);
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    if (resp.status.error_code == 0) {

                                    } else {
                                        Toast.makeText(getActivity(), resp.status.error_message, Toast.LENGTH_LONG).show();
                                        mLiveHelper.perpareQuitRoom(true);
                                        if (isPushed) {
                                            mLiveHelper.stopPushAction();
                                        }
                                    }
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            response.body().close();
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    // loading dialog
//                                    loadingDialogFragment.dismiss();
                                }
                            });
                        }
                    }
                });

    }

    private void talkieLiveRoomDismiss() {

        // loading dialog
//        final LoadingDialogFragment loadingDialogFragment = new LoadingDialogFragment();
//        loadingDialogFragment.show(getSupportFragmentManager(), "loading");

        Log.e(TAG, "dismiss :" + TalkieApi.getHost() + TalkieApi.PATH_LESSON + mLesson.id + "/dismiss");

        OkHttpManager.client.newCall(
                new Request.Builder().url(TalkieApi.getHost() + TalkieApi.PATH_LESSON + mLesson.id + "/dismiss")
                        //
                        .post(RequestBody.create(OkHttpManager.JSON, "{}"))
                        .build())
                //
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getActivity(), getText(R.string.network_exception), Toast.LENGTH_LONG).show();
                                // loading dialog
//                                    loadingDialogFragment.dismiss();
                            }
                        });
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (!response.isSuccessful()) {
                            return;
                        }
                        String jsonStr = response.body().string();
                        Log.e(TAG, "dismiss resp:" + jsonStr);

                        try {
                            final BaseResponse resp = new Gson().fromJson(jsonStr, BaseResponse.class);
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    if (resp.status.error_code == 0) {

                                    } else {
                                        Toast.makeText(getActivity(), resp.status.error_message, Toast.LENGTH_LONG).show();
                                    }
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            response.body().close();
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    // loading dialog
//                                    loadingDialogFragment.dismiss();
                                }
                            });
                        }
                    }
                });

    }

    private void talkieLiveRoomLeave() {

        // loading dialog
//        final LoadingDialogFragment loadingDialogFragment = new LoadingDialogFragment();
//        loadingDialogFragment.show(getSupportFragmentManager(), "loading");

        Log.e(TAG, "leave :" + TalkieApi.getHost() + TalkieApi.PATH_LESSON + mLesson.id + "/leave");

        OkHttpManager.client.newCall(
                new Request.Builder().url(TalkieApi.getHost() + TalkieApi.PATH_LESSON + mLesson.id + "/leave")
                        //
                        .post(RequestBody.create(OkHttpManager.JSON, "{}"))
                        .build())
                //
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getActivity(), getText(R.string.network_exception), Toast.LENGTH_LONG).show();
                                // loading dialog
//                                    loadingDialogFragment.dismiss();
                            }
                        });
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (!response.isSuccessful()) {
                            return;
                        }
                        String jsonStr = response.body().string();

                        try {
                            final BaseResponse resp = new Gson().fromJson(jsonStr, BaseResponse.class);
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    if (resp.status.error_code == 0) {

                                    } else {
                                        Toast.makeText(getActivity(), resp.status.error_message, Toast.LENGTH_LONG).show();
                                    }
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            response.body().close();
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    // loading dialog
//                                    loadingDialogFragment.dismiss();
                                }
                            });
                        }
                    }
                });

    }

    private void kick() {
        mLiveHelper.getMemberList(new LiveHelper.MemberListCallback() {
            @Override
            public void onMember(List<TIMGroupMemberInfo> list) {
                final String[] arr = new String[list.size()];
                for (int i = 0; i < list.size(); i = i + 1) {
                    arr[i] = list.get(i).getUser();
                }
                new AlertDialog.Builder(getActivity())
                        .setTitle("踢人")
                        .setItems(arr, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                kick(arr[which]);
                            }
                        })
                        .show();
            }
        });
    }

    private void kick(String userId) {
        mLiveHelper.sendGroupMessage(Constants.AVIMCMD_MULTI_CANCEL_INTERACT, "" + userId);
    }

    private void getLessonStatus() {
        OkHttpManager.client.newCall(
                new Request.Builder().url(TalkieApi.getHost() + TalkieApi.PATH_LESSON + mLesson.id + "/status/")
                        .build())
                //
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (!response.isSuccessful()) {
                            return;
                        }
                        String jsonStr = response.body().string();

                        try {
                            final LessonStatusResp resp = new Gson().fromJson(jsonStr, LessonStatusResp.class);
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    if (resp.status.error_code == 0) {
                                        TextView tvHeadCount = (TextView) findViewById(R.id.tvHeadCount);
                                        tvHeadCount.setText("" + resp.student_count + " students in the room");
                                    }
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            response.body().close();
                        }
                    }
                });
    }

    private static class LessonStatusResp extends BaseResponse {
        int student_count;
    }


    private class LiveVpAdapter extends PagerAdapter {
        View viewContent;
        View viewRight;

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            viewContent = LayoutInflater.from(getActivity()).inflate(R.layout.view_live_content, container, false);
            viewRight = LayoutInflater.from(getActivity()).inflate(R.layout.view_live_right, container, false);
            initHintView();
            View view;
            switch (position) {
                case 0:
                    view = viewContent;
                    break;
                case 1:
                    view = viewRight;
                    break;
                default:
                    view = viewContent;
                    break;
            }


            ViewParent parent = view.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeView(view);
            }
            container.addView(view);

            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        private void initHintView() {
            RecyclerView rv = (RecyclerView) viewRight.findViewById(R.id.rv);
            rv.setLayoutManager(new LinearLayoutManager(getActivity()));
            rv.setAdapter(new LessonHintRvAdapter());
            if (mLesson.topic == null || mLesson.topic.topic_hints == null) {

            } else {
                ((LessonHintRvAdapter) rv.getAdapter()).setData(mLesson.topic.topic_hints);
            }

        }
    }


}
